<?php

// *******************
// *** 消息队列生产者
// *******************

namespace app\service;

use Webman\RedisQueue\Client;

class RedisQueueService
{
    public static ?RedisQueueService $_instance  = null;
    protected string                 $queue_name = 'converter_list';

    /**
     * @return RedisQueueService|mixed
     */
    public static function instance(): ?RedisQueueService
    {
        if (!static::$_instance) static::$_instance = new self();
        return static::$_instance;
    }

    /**
     * @param string $name
     * @return $this
     */
    public function setName(string $name = ''): RedisQueueService
    {
        $this->queue_name = $name;
        return $this;
    }

    /**
     * @return string
     */
    public function getName(): string
    {
        return $this->queue_name;
    }

    /**
     * 投递消息
     *
     * @param array $data
     * @param int   $delay
     * @return bool
     */
    public function _send(array $data = [], int $delay = 0): bool
    {
        $queue = $this->queue_name;
        Client::send($queue, $data, $delay);      // 投递延迟消息，消息会在 $delay 秒后处理
        return true;
    }
}